package com.company.controller;

import com.company.common.Constants;
import com.company.model.result.BaseResult;
import com.company.model.result.CaptchaImageResult;
import com.company.service.ICacheService;
import com.company.utils.ImageCodeUtil;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by tu on 2017/7/3.
 */
@RestController
@RequestMapping(value = "/captcha")
public class CaptchaController {

  @Autowired
  ICacheService cacheService;

  /**
   * 生成验证码图片(一分钟失效)
   */
  @RequestMapping(method = RequestMethod.GET)
  public BaseResult imageCaptcha() throws IOException {
    CaptchaImageResult r = ImageCodeUtil.createImageCode();
    String key = r.id + Constants.SALT_CAPTCHA_IMAGE;
    String redisResult = cacheService.setex(key, Constants.REDIS_EXTIME_CAPTCHA_IMAGE, r.getCaptcha());
    BaseResult<CaptchaImageResult> result = new BaseResult();
    result.setData(r);
    return result;
  }
}